import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15
import QtQuick.Layouts 1.15

SubView0{
    id: root
    width: 252
    height: 325
    property color btnColor: "#7080C0"
    property int btnHeight: 30
    property int btnWidth: 70
    property int fontSize: 25

    signal stopTimer;
    signal startTimer;
    onStopTimer: {
        collectWeightTimer.running=false
    }
    onStartTimer: {
        collectWeightTimer.running=true
    }
    Component.onDestruction: {
        collectWeightTimer.destroy()
    }

    Component.onCompleted: {
        
    }
    function resetTotalValue()
    {
        var value = parseFloat(txtInputValue.text);
        var newWeight = parseFloat(mymodbus.weightNew);
        if (!isNaN(value)) {
            txtTotalValue.text = (value * newWeight).toFixed(2);
        } else {
            txtTotalValue.text = "";
        }
    }
    Rectangle{
        width:250
        height: 70
        color:"transparent"
        GridLayout{
            anchors.centerIn: parent
            columns: 2
            rows:2

            Button
            {
                Layout.row:0
                Layout.column:0
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                text: qsTr("单价")
                font.pixelSize: fontSize
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {

                }
            }
            Rectangle {
                id:recUnitPrice
                Layout.row:0
                Layout.column:1
                //Layout.preferredWidth: buttonWidth
                Layout.preferredHeight: btnHeight
                width:180
                height:btnHeight
                color: "transparent"
                border.width: 0
                border.color: "white"
                // 绘制一条边框
                Shape {
                    anchors.fill: parent
                    ShapePath {
                        strokeWidth: 3
                        strokeColor: "white"
                        strokeStyle: ShapePath.SolidLine
                        startX: 0
                        startY: recUnitPrice.height
                        PathLine {
                            x: recUnitPrice.width-30
                            y: recUnitPrice.height
                        }
                    }
                }
                TextInput{
                    anchors.fill: parent
                    id:txtInputValue
                    color: "white"
                    //text: currentText
                    //echoMode: TextInput.Password
                    font.pixelSize: fontSize
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignBottom
                    onTextChanged: {
                        resetTotalValue()
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {

                        }
                    }
                }
                 // 添加英镑符号
                Text {
                    id: currencySymbol
                    text: "£"
                    color: "white"
                    font.pixelSize: fontSize
                    anchors.right: parent.right
                    anchors.rightMargin: 0
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            Button
            {
                Layout.row:1
                Layout.column:0
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                text: qsTr("总价")
                font.pixelSize: fontSize
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                }
            }
            Rectangle {
                id:recTotalPrice
                Layout.row:1    
                Layout.column:1
                //Layout.preferredWidth: buttonWidth
                Layout.preferredHeight: btnHeight
                width:180
                height:btnHeight
                color: "transparent"
                border.width: 0
                border.color: "white"
                // 绘制一条边框
                Shape {
                    anchors.fill: parent
                    ShapePath {
                        strokeWidth: 3
                        strokeColor: "white"
                        strokeStyle: ShapePath.SolidLine
                        startX: 0
                        startY: recTotalPrice.height
                        PathLine {
                            x: recTotalPrice.width-30
                            y: recTotalPrice.height
                        }
                    }
                }
                Text{
                    id:txtTotalValue
                    anchors.fill: parent                    
                    color: "white"
                    font.pixelSize: fontSize
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignBottom
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            // miniKeyBoard.lineValue = "";
                            // miniKeyBoard.textInput=txtInputValue;
                        }
                    }
                }
                 // 添加英镑符号
                Text {
                    id: currencySymbol1
                    text: "£"
                    color: "white"
                    font.pixelSize: fontSize
                    anchors.right: parent.right
                    anchors.rightMargin: 0
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
    }

    MiniKeyBoardVertical
    {
        id: miniKeyBoardVertical
        x: (root.width - 200) / 2
        y: 80
        isNumber: true
        isDecimal: true
        textInput: txtInputValue
    }
    Button
    {
        y: 290
        x: (root.width - 200) / 2
        width: 200
        height: 40        
        text: qsTr("打印")
        font.pixelSize: 30
        background: Rectangle{
            color: btnColor
                }
        onClicked: {
            mymodbus.slotPrintReceipt(parseFloat(mymodbus.weightNew),parseFloat(txtInputValue.text),parseFloat(txtTotalValue.text));
            // miniKeyBoard.lineValue = "";
            // miniKeyBoard.textInput=confirmPwd;
        }
    }
    Timer{
        id: collectWeightTimer
        running: true
        repeat: true
        interval: mymodbus.collectTime
        onTriggered: {
            resetTotalValue()
        }
    }
}

